"""
@Author  : 康帅
@Time    : 2021/3/10 11:16
@Function: 热水器设计v1.0
"""

from abc import ABCMeta, abstractmethod
"""引入ABCMeta，abstractmethod来定义抽象类和抽象方法"""


class WaterHeater:

    def __init__(self):
        self.__observers = []
        self.__temperature = 25

    def get_temperature(self):
        """ 获取温度 """
        return self.__temperature

    def set_temperature(self, temperature):
        self.__temperature = temperature
        print("当前温度是：" + str(self.__temperature) + "℃")
        self.notifies()

    def add_observer(self, observer):
        """ 添加观察者 """
        self.__observers.append(observer)

    def notifies(self):
        """ 通知 """
        for o in self.__observers:
            o.update(self)


class ObServer(metaclass=ABCMeta):
    """洗澡模式个饮用模式的父类"""

    @abstractmethod
    def update(self, water_heaters):
        pass


class WashingMode(ObServer):
    """该模式用于洗澡"""

    def update(self, water_heaters):
        if water_heaters.get_temperature() >= 50 and water_heaters.get_temperature() < 70:
            print("水已烧好！可以洗澡")


class DrinkingMode(ObServer):
    """该模式用于饮用"""

    def update(self, water_heaters):
        if water_heaters.get_temperature() >= 100:
            print("水已烧开！可以饮用")


if __name__ == '__main__':
    heater = WaterHeater()
    washing_ob_server = WashingMode()
    drinking_ob_server = DrinkingMode()
    heater.add_observer(washing_ob_server)
    heater.add_observer(drinking_ob_server)
    heater.set_temperature(40)
    heater.set_temperature(60)
    heater.set_temperature(100)
